package com.ittony.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ittony.health.domain.CheckGroup;
import com.ittony.health.domain.CheckItem;
import com.ittony.health.entity.PageResult;
import com.ittony.health.entity.QueryPageBean;
import com.ittony.health.mapper.CheckGroupMapper;
import com.ittony.health.mapper.CheckItemMapper;
import com.ittony.health.mapper.SetMealMapper;
import com.ittony.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    @Override
    public void add(CheckGroup checkGroup,Integer[] checkItemIds) {
        checkGroupMapper.add(checkGroup);
        if(checkItemIds != null && checkItemIds.length > 0){
            //System.out.println(checkGroup);
            Integer checkGroupId = checkGroup.getId();
            checkGroupMapper.addCheckGroupCheckItems(checkGroupId,checkItemIds);
        }
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupMapper.findByPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public CheckGroup findCheckGroupById(Integer id) {
       return checkGroupMapper.findCheckGroupById(id);
    }

    @Override
    public CheckGroup findById(Integer id) {
        //List<Integer> checkItemIds = checkGroupMapper.findCheckItemId(id);
        //List<CheckItem> checkItemList = checkItemMapper.findByIds(checkItemIds);
        //return checkItemList;
        CheckGroup checkGroup = checkGroupMapper.findById(id);
        return checkGroup;
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupMapper.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void delete(Integer checkGroupId) {
        List<Integer> checkItemIds = checkItemMapper.getIdsByCheckGroupId(checkGroupId);
        List<Integer> setMealIds = setMealMapper.getIdsByCheckGroupId(checkGroupId);
        if((checkItemIds.isEmpty() || checkItemIds == null) && (setMealIds.isEmpty() || setMealIds == null)){
            checkGroupMapper.delete(checkGroupId);
        }else{
            throw new RuntimeException("存在已关联至本检查组的检查项或检查套餐");
        }

    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkItemIds) {
        checkGroupMapper.update(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        checkGroupMapper.deleteCheckGroupCheckItems(checkGroupId);
        if(checkItemIds != null && checkItemIds.length > 0){
            checkGroupMapper.addCheckGroupCheckItems(checkGroupId,checkItemIds);
        }

    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }
}
